package b2.player
{
	import b2.config.*;
	
	public class TankInfo
	{
		public var id:int;
		public var ownerId:int;
		public var index:int;
		public var basicLevel:int;
		public var strengthenLevel:int;
		
		public function TankInfo(tankXML:XML = null)
		{
			if (tankXML != null)
			{
				this.id = tankXML.tankId;
				this.ownerId = tankXML.ownerId;
				this.index = tankXML.tankIndex;
				this.basicLevel = tankXML.basicLevel;
				this.strengthenLevel = tankXML.strengthenLevel;
			}
		}
		
		public function get healthPointBase():int
		{
			var tankConfig:TankConfig = Config.objectConfigs[index];
			return parseInt(tankConfig.healthPoint[basicLevel * Config.basicConfig.tankStrengthenLevel.length + strengthenLevel]);
		}
		
		public function get attackPointBase():int
		{
			var tankConfig:TankConfig = Config.objectConfigs[index];
			return parseInt(tankConfig.attackPoint[basicLevel * Config.basicConfig.tankStrengthenLevel.length + strengthenLevel]);
		}
		
		public function get criticalAttackRatioBase():int
		{
			var tankConfig:TankConfig = Config.objectConfigs[index];
			return parseInt(tankConfig.criticalAttackRatio[basicLevel * Config.basicConfig.tankStrengthenLevel.length + strengthenLevel]);
		}
		
		public function get armPointBase():int
		{
			var tankConfig:TankConfig = Config.objectConfigs[index];
			return parseInt(tankConfig.armPoint[basicLevel * Config.basicConfig.tankStrengthenLevel.length + strengthenLevel]);
		}
		
		public function get energyPointBase():int
		{
			var tankConfig:TankConfig = Config.objectConfigs[index];
			return parseInt(tankConfig.energyPoint[basicLevel * Config.basicConfig.tankStrengthenLevel.length + strengthenLevel]);
		}
		
		public function get healPointEquip():int
		{
			var owner:PlayerInfo = PlayerInfoManager.getInstance().players[ownerId];
			var healthPoint:int = 0;
			for (var comIndex:String in owner.objPackage.objectList)
			{
				var comIndexArr:Array = comIndex.split("_");
				var objectIndex:int = comIndexArr[0];
				var basicLevel:int = comIndexArr[1];
				var strengthenLevel:int = comIndexArr[2];
				var tankId:int = comIndexArr[3];
				if (tankId == id) 
				{
					var equipConfig:EquipConfig = Config.objectConfigs[objectIndex];
					if (equipConfig.healthPointPlus != null)
						healthPoint += parseInt(equipConfig.healthPointPlus[basicLevel * 5 + strengthenLevel]);
				}
			}
			comIndex = getEquipByPosition(5);
			if (comIndex != null)
			{
				comIndexArr = comIndex.split("_");
				objectIndex = comIndexArr[0];
				basicLevel = comIndexArr[1];
				strengthenLevel = comIndexArr[2];
				equipConfig = Config.objectConfigs[objectIndex];
				if (equipConfig.healthPointPlus != null)
					healthPoint += parseInt(equipConfig.healthPointPlus[basicLevel * 5 + strengthenLevel]);
			}  
			return healthPoint;
		}

		public function get attackPointEquip():int
		{
			var owner:PlayerInfo = PlayerInfoManager.getInstance().players[ownerId];
			var attackPoint:int = 0;
			for (var comIndex:String in owner.objPackage.objectList)
			{
				var comIndexArr:Array = comIndex.split("_");
				var objectIndex:int = comIndexArr[0];
				var basicLevel:int = comIndexArr[1];
				var strengthenLevel:int = comIndexArr[2];
				var tankId:int = comIndexArr[3];
				if (tankId == id) 
				{
					var equipConfig:EquipConfig = Config.objectConfigs[objectIndex];
					if (equipConfig.attackPointPlus != null)
						attackPoint += parseInt(equipConfig.attackPointPlus[basicLevel * 5 + strengthenLevel]);
				}
			}
			comIndex = getEquipByPosition(5);
			if (comIndex != null)
			{
				comIndexArr = comIndex.split("_");
				objectIndex = comIndexArr[0];
				basicLevel = comIndexArr[1];
				strengthenLevel = comIndexArr[2];
				equipConfig = Config.objectConfigs[objectIndex];
				if (equipConfig.attackPointPlus != null)
					attackPoint += parseInt(equipConfig.attackPointPlus[basicLevel * 5 + strengthenLevel]);
			}  
			return attackPoint;
		}

		public function get criticalAttackRatioEquip():int
		{
			var owner:PlayerInfo = PlayerInfoManager.getInstance().players[ownerId];
			var criticalAttackRatio:int = 0;
			for (var comIndex:String in owner.objPackage.objectList)
			{
				var comIndexArr:Array = comIndex.split("_");
				var objectIndex:int = comIndexArr[0];
				var basicLevel:int = comIndexArr[1];
				var strengthenLevel:int = comIndexArr[2];
				var tankId:int = comIndexArr[3];
				if (tankId == id) 
				{
					var equipConfig:EquipConfig = Config.objectConfigs[objectIndex];
					if (equipConfig.caRatioPlus != null)
						criticalAttackRatio += parseInt(equipConfig.caRatioPlus[basicLevel * 5 + strengthenLevel]);
				}
			}
			comIndex = getEquipByPosition(5);
			if (comIndex != null)
			{
				comIndexArr = comIndex.split("_");
				objectIndex = comIndexArr[0];
				basicLevel = comIndexArr[1];
				strengthenLevel = comIndexArr[2];
				equipConfig = Config.objectConfigs[objectIndex];
				if (equipConfig.caRatioPlus != null)
					criticalAttackRatio += parseInt(equipConfig.caRatioPlus[basicLevel * 5 + strengthenLevel]);
			}  
			return criticalAttackRatio;
		}

		public function get armPointEquip():int
		{
			var owner:PlayerInfo = PlayerInfoManager.getInstance().players[ownerId];
			var armPoint:int = 0;
			for (var comIndex:String in owner.objPackage.objectList)
			{
				var comIndexArr:Array = comIndex.split("_");
				var objectIndex:int = comIndexArr[0];
				var basicLevel:int = comIndexArr[1];
				var strengthenLevel:int = comIndexArr[2];
				var tankId:int = comIndexArr[3];
				if (tankId == id) 
				{
					var equipConfig:EquipConfig = Config.objectConfigs[objectIndex];
					if (equipConfig.armPointPlus != null)
						armPoint += parseInt(equipConfig.armPointPlus[basicLevel * 5 + strengthenLevel]);
				}
			}
			comIndex = getEquipByPosition(5);
			if (comIndex != null)
			{
				comIndexArr = comIndex.split("_");
				objectIndex = comIndexArr[0];
				basicLevel = comIndexArr[1];
				strengthenLevel = comIndexArr[2];
				equipConfig = Config.objectConfigs[objectIndex];
				if (equipConfig.armPointPlus != null)
					armPoint += parseInt(equipConfig.armPointPlus[basicLevel * 5 + strengthenLevel]);
			}  
			return armPoint;
		}

		public function get energyPointEquip():int
		{
			var owner:PlayerInfo = PlayerInfoManager.getInstance().players[ownerId];
			var energyPoint:int = 0;
			for (var comIndex:String in owner.objPackage.objectList)
			{
				var comIndexArr:Array = comIndex.split("_");
				var objectIndex:int = comIndexArr[0];
				var basicLevel:int = comIndexArr[1];
				var strengthenLevel:int = comIndexArr[2];
				var tankId:int = comIndexArr[3];
				if (tankId == id) 
				{
					var equipConfig:EquipConfig = Config.objectConfigs[objectIndex];
					if (equipConfig.energyPointPlus != null)
						energyPoint += parseInt(equipConfig.energyPointPlus[basicLevel * 5 + strengthenLevel]);
				}
			}
			comIndex = getEquipByPosition(5);
			if (comIndex != null)
			{
				comIndexArr = comIndex.split("_");
				objectIndex = comIndexArr[0];
				basicLevel = comIndexArr[1];
				strengthenLevel = comIndexArr[2];
				equipConfig = Config.objectConfigs[objectIndex];
				if (equipConfig.energyPointPlus != null)
					energyPoint += parseInt(equipConfig.energyPointPlus[basicLevel * 5 + strengthenLevel]);
			}  
			return energyPoint;
		}

		public function get healthPoint():int
		{
			return healthPointBase + healPointEquip;
		}
		
		public function get attackPoint():int
		{
			return attackPointBase + attackPointEquip;
		}
		
		public function get criticalAttackRatio():int
		{
			return criticalAttackRatioBase + criticalAttackRatioEquip;
		}
		
		public function get armPoint():int
		{
			return armPointBase + armPointEquip;
		}
		
		public function get energyPoint():int
		{
			return energyPointBase + energyPointEquip;
		}
		
		public function get moveVelocity():int
		{
			var tankConfig:TankConfig = Config.objectConfigs[index];
			return tankConfig.moveVelocity;
		}
		
		public function get fireCooldown():int
		{
			var tankConfig:TankConfig = Config.objectConfigs[index];
			return tankConfig.fireCooldown;
		}
		
		public function get fightPointEquip():int
		{
			var fp:int = 0;
			//装备战力
			var owner:PlayerInfo = PlayerInfoManager.getInstance().players[ownerId];
			for (var comIndex:String in owner.objPackage.objectList)
			{
				var comIndexArr:Array = comIndex.split("_");
				var objectIndex:int = comIndexArr[0];
				var basicLevel:int = comIndexArr[1];
				var strengthenLevel:int = comIndexArr[2];
				var tankId:int = comIndexArr[3]; 
				if (tankId == id)
				{
					var equipConfig:EquipConfig = Config.objectConfigs[objectIndex] as EquipConfig;
					fp += parseInt(equipConfig.fightPoint[basicLevel * 5 + strengthenLevel]);
				}
			}
			//属性套装
			comIndex = getEquipByPosition(5);
			if (comIndex != null)
			{
				comIndexArr = comIndex.split("_");
				objectIndex = comIndexArr[0];
				basicLevel = comIndexArr[1];
				strengthenLevel = comIndexArr[2];
				equipConfig = Config.objectConfigs[objectIndex] as EquipConfig;
				fp += parseInt(equipConfig.fightPoint[basicLevel * 5 + strengthenLevel]);
			}
			//整体套装
			comIndex = getEquipByPosition(9);
			if (comIndex != null)
			{
				comIndexArr = comIndex.split("_");
				objectIndex = comIndexArr[0];
				basicLevel = comIndexArr[1];
				strengthenLevel = comIndexArr[2];
				equipConfig = Config.objectConfigs[objectIndex] as EquipConfig;
				fp += parseInt(equipConfig.fightPoint[basicLevel * 5 + strengthenLevel]);
			}
			return fp;
		}
		
		public function get fightPointBase():int
		{
			//坦克战力
			var tankConfig:TankConfig = Config.objectConfigs[index];
			return parseInt(tankConfig.fightPoint[this.basicLevel * 5 + this.strengthenLevel]);			
		}
		
		public function get fightPoint():int
		{
			return fightPointBase + fightPointEquip;
		}
		
		public function getEquipByPosition(position:int):String
		{
			var owner:PlayerInfo = PlayerInfoManager.getInstance().players[ownerId];
			var suitIndex:int = -1;
			var suitCount:int = 0;
			var suitQuality:int = 25;
			for (var comIndex:String in owner.objPackage.objectList)
			{
				var comIndexArr:Array = comIndex.split("_");
				var objectIndex:int = comIndexArr[0];
				var basicLevel:int = comIndexArr[1];
				var strengthenLevel:int = comIndexArr[2];
				var tankId:int = comIndexArr[3]; 
				if (tankId == id)
				{
					var equipConfig:EquipConfig = Config.objectConfigs[objectIndex] as EquipConfig;
					//套装
					if (position == 5 || position == 9)
					{
						if (equipConfig.position < position && (suitIndex == -1 || suitIndex != -1 && equipConfig.suitIndex == suitIndex))
						{
							suitCount++;
							suitIndex = equipConfig.suitIndex;
							if (basicLevel * 5 + strengthenLevel < suitQuality)
								suitQuality = basicLevel * 5 + strengthenLevel;
						}
					}
					//单件
					else
					{
						if (equipConfig.position == position)
						{
							return comIndex;
						}
					}
				}
			}
			if (position == 5 && suitCount == 5 || position == 9 && suitCount == 8)
			{
				objectIndex = 20000 + 10 * suitIndex + position;
				basicLevel = suitQuality / 5;
				strengthenLevel = suitQuality % 5;
				return objectIndex + "_" + basicLevel + "_" + strengthenLevel + "_" + this.id;
			}
			return null;
		}

		//获取能装备的数目
		public function getAvailableSlotNum():int
		{
			var count:int = 0;
			for (var i:int = 0; i < TankConfig.EQUIP_SLOT_AVAILABLE.length; i++)
			{
				if (TankConfig.EQUIP_SLOT_AVAILABLE[i] <= basicLevel)
				{
					count++;
				}
			}
			return count;
		}
	}
}